package com.gg.user.gguser.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.gg.header.getHeaderUser;
import com.gg.user.gguser.dao.GgBusinessOpportunityDaoMapper;
import com.gg.user.gguser.dao.GgInformationDaoMapper;
import com.gg.user.gguser.dataobject.GgInformationDataobject;
import com.gg.user.gguser.dataobject.GgItemsOnlyDataobject;
import com.gg.user.gguser.service.GgDeletcInformationService;
import com.gg.user.gguser.service.GgInformationService;
import com.gg.user.gguser.service.GgItemsOnlyService;
import com.gg.vo.return_format;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class GgInformationServiceImpl implements GgInformationService {

    @Autowired
    private GgInformationDaoMapper ggInformationDao;

    @Autowired
    private GgItemsOnlyService ggItemsOnlyService;

    @Autowired
    private GgDeletcInformationService ggDeletcInformationService;

    @Autowired
    private GgBusinessOpportunityDaoMapper ggBusinessOpportunityDaoMapper;

    @Override
    public Object getInformationSelectList(HttpServletResponse response, HttpServletRequest request,Integer page, Integer pageNumber, String name, Integer type, Integer state, String remarks) {
        if(page != null && page > 0 && pageNumber != null && pageNumber > 0 ){
            try {
                // 获取用户信息
                // 从cookice信息中过去用户信息 在这里只用要id就好
                String cookiceUser = getHeaderUser.getHader(response,request,"gg");
                String ggId= JSONObject.parseObject(cookiceUser).getString("ggId");
                // 数据列表
                name = name!=null && name.equals("") ? null:name;
                type = type!=null && type.equals("") ? null:type;
                state = state!=null && state.equals("") ? null:state;
                remarks = remarks!=null && remarks.equals("") ? null:remarks;

                //获取一共多少个
                Integer getInformationSelectCount = ggInformationDao.getInformationSelectCount(ggId, name, type, state, remarks);
                List<GgInformationDataobject> getInformationSelectList = ggInformationDao.getInformationSelectList((page-1)*pageNumber, pageNumber, ggId, name, type, state, remarks);
                return return_format.return_format_All(0,return_format.return_format_data_map(page, (int) Math.ceil(getInformationSelectCount/pageNumber),pageNumber,getInformationSelectCount,getInformationSelectList),"");
            }catch (Exception e){
                return return_format.return_format_error(100003,"服务有问题了");
            }
        }else {
            return return_format.return_format_error(100002,"用户自有数据是允许爬虫，但是参数不要瞎传！");
        }
    }

    @Override
    public Object getInformationSelectItems(String itemsId) {
        if(itemsId != null) {
            try {
                return return_format.return_format_All(0, ggInformationDao.getInformationSelectItems(itemsId),"");
            }catch (Exception e){
                return return_format.return_format_error(100003,"服务有问题了");
            }
        }else {
            return return_format.return_format_error(100002,"用户自有数据是允许爬虫，但是参数不要瞎传！");
        }
    }

    /**
     * 单个增加信息
     */
    @Override
    public Object addInformationAll(HttpServletResponse response, HttpServletRequest request, String name, String sex, String position, String phone, String mailbox, String qq, String weChat, String provid, String cityid, String areaid, String address, Integer level, Integer state, String remarks, String username, String fax, String contact,String zipcode,String website,String source) throws Exception {
        if(name != null && name != "" && username != null && username != "" && address != null && address != "" && phone!=null  && phone != ""){
            try {
                String cookiceUser = getHeaderUser.getHader(response,request,"gg");
                String ggIdUser= JSONObject.parseObject(cookiceUser).getString("ggId");
                // 弄一个商机、信息公共唯一标识符
                GgItemsOnlyDataobject ggItemsOnlyDataobjectToken = ggItemsOnlyService.judgeItemsOnly(response,request,"信息");
                if (ggItemsOnlyDataobjectToken != null){
                    ggInformationDao.addInformationOpportunity(name,provid,cityid,areaid,address,contact,username,state,level,sex,position,phone,fax,mailbox,qq,weChat,remarks,ggIdUser,ggIdUser,ggItemsOnlyDataobjectToken.getGgIoOnly(),zipcode,website,source);
                    return return_format.return_format_All(0,"","");
                }else {
                    return return_format.return_format_error(100003,"插入失败请稍后重试！");
                }
            }catch (Exception e){
                return return_format.return_format_error(100003,"插入失败请稍后重试！");
            }
        }else {
            return return_format.return_format_error(100002,"用户自有数据是允许爬虫，但是参数不要瞎传！");
        }
    }

    /**
     * 修改商机
     */
    @Override
    public Object updateInformation(HttpServletResponse response, HttpServletRequest request, String ggItemsToken, String ggIName, String ggIProvince, String ggICity, String ggIAreaid, String ggIDetailed, String ggIHandle, String ggIContact, Integer ggIState, Integer ggILevel, String ggIGender, String ggIPosition, String ggITelephone, String ggIFax, String ggIMailbox, String ggIQq, String ggIWechat,String ggIZipcode,String ggIWebsite,String ggISource) throws Exception{
        if(ggIName != null || ggIName != "" || ggIContact != null || ggIContact != "" || ggITelephone != null || ggITelephone != "" || ggIDetailed!=null  || ggIDetailed != ""){
            String cookiceUser = getHeaderUser.getHader(response,request,"gg");
            String ggIdUser= JSONObject.parseObject(cookiceUser).getString("ggId"); // 获取到用户id,String ggIZipcode,String ggIWebsite,String ggISource
            ggInformationDao.updateInformation(ggItemsToken, ggIName, ggIProvince, ggICity, ggIAreaid, ggIDetailed, ggIHandle ,ggIContact, ggIState ,ggILevel ,ggIGender ,ggIPosition ,ggITelephone ,ggIFax ,ggIMailbox ,ggIQq, ggIWechat, ggIdUser, ggIZipcode, ggIWebsite, ggISource);
            return return_format.return_format_All(0,"","");
        }else {
            return return_format.return_format_error(100002,"用户自有数据是允许爬虫，但是参数不要瞎传！");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Object delectInformation(HttpServletResponse response, HttpServletRequest request, Integer id) throws Exception{
        if(id != null || id<=0 ){
            String cookiceUser = getHeaderUser.getHader(response,request,"gg");
            String ggIdUser= JSONObject.parseObject(cookiceUser).getString("ggId"); // 获取到用户id
            GgInformationDataobject ggInformationDataobject= ggInformationDao.getBusinessOpportunity(id);// 获取信息
            if(ggInformationDataobject==null){
                return return_format.return_format_error(100002,"删除商机不存在！");
            }else {
                // 移入删除信息
                ggDeletcInformationService.addDeletcInformationService(ggInformationDataobject.getGgItemsToken(), ggInformationDataobject.getGgIName(), ggInformationDataobject.getGgIProvince(), ggInformationDataobject.getGgICity(), ggInformationDataobject.getGgIDetailed(), ggInformationDataobject.getGgIHandle(), ggInformationDataobject.getGgIContact(), ggInformationDataobject.getGgIState(), ggInformationDataobject.getGgILevel(), ggInformationDataobject.getGgIGender(), ggInformationDataobject.getGgIPosition(), ggInformationDataobject.getGgITelephone(), ggInformationDataobject.getGgIFax(), ggInformationDataobject.getGgIMailbox(), ggInformationDataobject.getGgIQq(), ggInformationDataobject.getGgIWechat(), ggInformationDataobject.getGgIRemarks(), ggInformationDataobject.getGgId(), ggIdUser, ggInformationDataobject.getGgITime(), ggIdUser, ggInformationDataobject.getGgIAreaid(),ggInformationDataobject.getGgIZipcode(),ggInformationDataobject.getGgIWebsite(),ggInformationDataobject.getGgISource());
                ggInformationDao.delectGgInformationDataobjectItems(id);
                return return_format.return_format_All(0,"","");
            }
        }else {
            return return_format.return_format_error(100002,"用户自有数据是允许爬虫，但是参数不要瞎传！");
        }
    }

    /**
     * 批量删除
     * @param response
     * @param request
     * @param listid
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Object delectInformationAll(HttpServletResponse response, HttpServletRequest request, String listid) throws Exception {
        List id_list = JSON.parseObject(listid,new TypeReference<List>(){});
        String cookiceUser = getHeaderUser.getHader(response,request,"gg");
        String ggIdUser= JSONObject.parseObject(cookiceUser).getString("ggId"); // 获取到用户id
        Integer yes = 0;  // 移动成功
        Integer not = 0;  // 没有这个信息
        Integer error = 0; // 错误
        if(id_list.size()!=0){
            try {
                for (int i = 0;i < id_list.size(); i++){
                    try {
                        Integer id = Integer.parseInt(String.valueOf(id_list.get(i))) ;// id
                        GgInformationDataobject ggInformationDataobject= ggInformationDao.getBusinessOpportunity(id);// 获取信息
                        if(ggInformationDataobject==null){
                            not = not + 1;
                        }else {
                            // 移入删除信息
                            ggDeletcInformationService.addDeletcInformationService(ggInformationDataobject.getGgItemsToken(), ggInformationDataobject.getGgIName(), ggInformationDataobject.getGgIProvince(), ggInformationDataobject.getGgICity(), ggInformationDataobject.getGgIDetailed(), ggInformationDataobject.getGgIHandle(), ggInformationDataobject.getGgIContact(), ggInformationDataobject.getGgIState(), ggInformationDataobject.getGgILevel(), ggInformationDataobject.getGgIGender(), ggInformationDataobject.getGgIPosition(), ggInformationDataobject.getGgITelephone(), ggInformationDataobject.getGgIFax(), ggInformationDataobject.getGgIMailbox(), ggInformationDataobject.getGgIQq(), ggInformationDataobject.getGgIWechat(), ggInformationDataobject.getGgIRemarks(), ggInformationDataobject.getGgId(), ggIdUser, ggInformationDataobject.getGgITime(), ggIdUser, ggInformationDataobject.getGgIAreaid(),ggInformationDataobject.getGgIZipcode(),ggInformationDataobject.getGgIWebsite(),ggInformationDataobject.getGgISource());
                            ggInformationDao.delectGgInformationDataobjectItems(id);
                            yes = yes + 1;
                        }
                    }catch (Exception e){
                        error = error + 1;
                    }
                }
                Map dataMap = new HashMap();
                dataMap.put("yes", yes);
                dataMap.put("not", not);
                dataMap.put("error", error);
                return return_format.return_format_All(0, dataMap,"");
            }catch (Exception e){
                return return_format.return_format_error(100002,"服务有问题了！");
            }
        }else {
            return return_format.return_format_error(100002,"用户自有数据是允许爬虫，但是参数不要瞎传！");
        }

    }

    /**
     * 批量 加入商机
     * @param response
     * @param request
     * @param listid
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Object removebusinessAll(HttpServletResponse response, HttpServletRequest request, String listid) throws Exception {
        List id_list = JSON.parseObject(listid,new TypeReference<List>(){});
        String cookiceUser = getHeaderUser.getHader(response,request,"gg");
        String ggId= JSONObject.parseObject(cookiceUser).getString("ggId"); // 获取到用户id
        Integer yes = 0;  // 移动成功
        Integer not = 0;  // 没有这个信息
        Integer error = 0; // 错误
        if(id_list.size()!=0){
            for (int i = 0;i < id_list.size(); i++){
                try {
                    Integer id = Integer.parseInt(String.valueOf(id_list.get(i))) ;// id
                    GgInformationDataobject ggInformationDataobject= ggInformationDao.getBusinessOpportunity(id);// 获取信息
                    if(ggInformationDataobject!=null){
                        // 插入商机
                        ggBusinessOpportunityDaoMapper.yaddBusinessOpportunity(ggInformationDataobject.getGgItemsToken(), ggInformationDataobject.getGgIName(), ggInformationDataobject.getGgIProvince(), ggInformationDataobject.getGgICity(), ggInformationDataobject.getGgIDetailed(), ggInformationDataobject.getGgIHandle(), ggInformationDataobject.getGgIContact(), ggInformationDataobject.getGgIState(), ggInformationDataobject.getGgILevel(), ggInformationDataobject.getGgIGender(), ggInformationDataobject.getGgIPosition(), ggInformationDataobject.getGgITelephone(), ggInformationDataobject.getGgIFax(), ggInformationDataobject.getGgIMailbox(), ggInformationDataobject.getGgIQq(), ggInformationDataobject.getGgIWechat(), ggInformationDataobject.getGgIRemarks(), ggInformationDataobject.getGgId(), ggId, ggInformationDataobject.getGgITime(),ggInformationDataobject.getGgIAreaid(),ggInformationDataobject.getGgIZipcode(),ggInformationDataobject.getGgIWebsite(),ggInformationDataobject.getGgISource());
                        // 从信息里面删除
                        ggInformationDao.delectGgInformationDataobjectItems(id);
                        yes = yes + 1;
                    }else {
                        not = not + 1;

                    }
                }catch (Exception e){
                    error = error + 1;
                }
            }
            Map dataMap = new HashMap();
            dataMap.put("yes", yes);
            dataMap.put("not", not);
            dataMap.put("error", error);
            return return_format.return_format_All(0, dataMap,"");
        }else {
            return return_format.return_format_error(100002,"用户自有数据是允许爬虫，但是参数不要瞎传！");
        }
    }

}
